﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace badminton.Pages
{
    public partial class TrainingSearchResult : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            BadmintonEntities bde = new BadmintonEntities();
            this.DropDownListTraingCityArea.Items.Add(new ListItem("不限", "-1"));
            foreach (CityArea ca in bde.CityArea)
            {
                this.DropDownListTraingCityArea.Items.Add(new ListItem(ca.name, ca.id.ToString()));
            }

            bindTrainingHallDropDown(-1);

            this.DropDownListTrainingCategory.Items.Add(new ListItem("不限", "-1"));
            foreach (TrainingCategory tc in bde.TrainingCategory)
            {
                this.DropDownListTrainingCategory.Items.Add(new ListItem(tc.name, tc.id.ToString()));
            }

            if (Session["TrainingSearch"] == null)
            {
                return;
            }

            List<Training> validTrainings = new List<Training>();
            foreach (Training tra in bde.Training.OrderByDescending(p=>p.startTime))
            {
                if (isTrainingOk(tra))
                {
                    validTrainings.Add(tra);
                }
            }

            this.Repeater1.DataSource = validTrainings;// bde.Hall.Where(p => validHallIds.IndexOf(p.id) != -1);
            this.Repeater1.DataBind();     
            
        }

        protected bool isTrainingOk(Training tra)
        {
            TrainingSearchSession ts = Session["TrainingSearch"] as TrainingSearchSession;
            if (ts.m_cityAreaId != -1)
            {
                if (tra.Hall == null)
                {
                    return false;
                }

                if (tra.Hall.cityAreaId != ts.m_cityAreaId)
                {
                    return false;
                }
            }

            if (ts.m_hallId!=-1)
            {
                if (tra.hallId!=ts.m_hallId)
                {
                    return false;
                }
            }

            if (ts.m_trainingCatId!=-1)
            {
                if (tra.traingCategoryId!= ts.m_trainingCatId)
                {
                    return false;
                }
            }

            if (ts.m_searchStr != null && ts.m_searchStr != "")
            {
                if (tra.name != null)
                {
                    if (tra.name.IndexOf(ts.m_searchStr) == -1)
                    {
                        return false;
                    }
                }
            }
            
            return true;
        }

        protected void OnTraingCityAreaChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(this.DropDownListTraingCityArea.SelectedValue);
            bindTrainingHallDropDown(id);

        }

        public string GetCityAreaName(object id)
        {
            int trainingId = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            Training tra = bde.Training.FirstOrDefault(p => p.id == trainingId);
            if (tra == null || tra.Hall == null || tra.Hall.CityArea == null)
            {
                return "";
            }

            return tra.Hall.CityArea.name;

        }

        public string GetHallName(object id)
        {
            int trainingId = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            Training tra = bde.Training.FirstOrDefault(p => p.id == trainingId);
            if (tra == null || tra.Hall == null )
            {
                return "";
            }

            return tra.Hall.name;

        }
        public string GetStartTime(object id)
        {
            int trainingId = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            Training tra = bde.Training.FirstOrDefault(p => p.id == trainingId);
            if (tra == null || tra.startTime == null )
            {
                return "";
            }

            return tra.startTime.Value.ToString("yyyy.MM.dd");

        }

        public string GetTraineeNumber(object id)
        {
            int trainingId = (int)id;
            BadmintonEntities bde = new BadmintonEntities();
            Training tra = bde.Training.FirstOrDefault(p => p.id == trainingId);
            if (tra == null || tra.Student == null)
            {
                return "";
            }

            int ret = tra.Student.Count;

            if (tra.requiredTraineeNum!=null)
            {
                if (ret < tra.requiredTraineeNum / 2) 
                {
                    ret = ret + tra.requiredTraineeNum.Value / 2;
                }
            }

            return ret.ToString();

        }

        protected void OnSearchTraining(object sender, CommandEventArgs e)
        {
            TrainingSearchSession trainingSearch = new TrainingSearchSession();
            trainingSearch.m_cityAreaId = Convert.ToInt32(this.DropDownListTraingCityArea.SelectedValue);
            trainingSearch.m_hallId = Convert.ToInt32(this.DropDownListTraingHall.SelectedValue);
            trainingSearch.m_trainingCatId = Convert.ToInt32(this.DropDownListTrainingCategory.SelectedValue);
            trainingSearch.m_searchStr = this.TextBoxTraingText.Text;

            Session["TrainingSearch"] = trainingSearch;

            Response.Redirect("~/pages/trainingsearchresult.aspx");

        }

        protected void bindTrainingHallDropDown(int cityareaid)
        {
            this.DropDownListTraingHall.Items.Clear();
            this.DropDownListTraingHall.Items.Add(new ListItem("不限", "-1"));
            if (cityareaid >= 0)
            {
                BadmintonEntities bde = new BadmintonEntities();
                foreach (Hall ha in bde.Hall.Where(p => p.cityAreaId == cityareaid))
                {
                    this.DropDownListTraingHall.Items.Add(new ListItem(ha.name, ha.id.ToString()));
                }
            }
        }
    }
}